// pages/line/add/add.ts
import type { formState, roadLineState } from '~/../typings/types/line/add'
import { doAdd } from '~/api/line'
import { back } from '~/utils/util'
import { doJoinInfo } from '~/api/join'
type infoState = {
  province: string,
  city: string,
  area: string
}
let loading = false
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {
      province: '',
      city: '',
      area: '',
    } as infoState,
    queryForm: {
      roadLineCustomerId: 0,
      roadLineDTO: []
    } as formState,
  },
  onIconTap(event: any) {
    const { n } = event.currentTarget.dataset;
    const { queryForm } = this.data
    queryForm.roadLineDTO.splice(n, 1)
    this.setData({
      queryForm
    })
  },
  onAreaPicker() {
    wx.navigateTo({
      url: '/pages/line/choose/city',
      events: {
        chooseEvent: (e: roadLineState[]) => {
          const { queryForm } = this.data
          if (!e[0].destArea) {
            queryForm.roadLineDTO = queryForm.roadLineDTO.filter(el => {
              const status = el.destProvince == e[0].destProvince && el.destCity == e[0].destCity
              return !status
            })
            queryForm.roadLineDTO.push(e[0])
          } else {
            queryForm.roadLineDTO = queryForm.roadLineDTO.filter(el => {
              const status = el.destProvince == e[0].destProvince && el.destCity == e[0].destCity && !el.destArea
              return !status
            })
            e.forEach(el => {
              const status = queryForm.roadLineDTO.some(item => item.destProvince == el.destProvince && item.destCity == el.destCity && item.destArea == el.destArea)
              if (!status) {
                queryForm.roadLineDTO.push(el)
              }
            })
          }
          this.setData({
            queryForm
          })
        }
      }
    })
  },
  toast(title: string) {
    wx.showToast({
      icon: 'none',
      title
    })
  },
  async handSubmit() {
    const { queryForm } = this.data
    if (queryForm.roadLineDTO.length == 0) {
      this.toast('请至少选择一个目的地')
      return
    }

    if (loading) return
    loading = true
    try {
      await doAdd(queryForm)
      this.toast('添加成功')
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.emit('success');
      setTimeout(() => {
        back()
      }, 300)
    } catch (error) {
      loading = false
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    const data = await doJoinInfo()
    if (data) {
      const info = {
        province: data.province,
        city: data.city,
        area: data.area,
      } as infoState
      const { queryForm } = this.data
      queryForm.roadLineCustomerId = data.id
      this.setData({
        info,
        queryForm
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})